package com.eleme.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eleme.dao.impl.UDaoFactory;
import com.eleme.pubs.Escape;
import com.eleme.service.IShopCartService;
import com.eleme.service.impl.ShopCartServiceImpl;

/**
 * Servlet implementation class SaveShopCart
 */
public class SaveShopCart extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().setAttribute("memberid", new Integer(1));
		int memberid = new Integer(request.getSession().getAttribute("memberid").toString()).intValue();
		String strcookie = null;
		Cookie[] cookies = request.getCookies();
		for(Cookie c : cookies){
			if(c.getName().equals("shopcart")) {
				System.out.println(c.getValue());
				strcookie = Escape.unescape(c.getValue());
				break;
			}
		}
		IShopCartService srv = new ShopCartServiceImpl(UDaoFactory.getInstance());
		srv.saveShopCart(strcookie, memberid);
		System.out.println(srv.readShopCartCookie(1));
		//response.sendRedirect("/asd");
	}

}
